﻿using System;
using System.Web.UI.WebControls;
using DataAccess.Models;

namespace KPI.Controls
{
	public partial class TheGroupKPIProgressData : System.Web.UI.UserControl
	{
		public Group TheGroup
		{
			get;
			set;
		}

		public DataAccess.Models.LoginUserInfo TheCurrentLoginUser
		{
			get
			{
				return KPISiteContext.Current.CurrentUser;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{

		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			if(TheGroup != null)
			{
				rptKPIProgressData.DataSource = TheGroup.ListKPIs;
				rptKPIProgressData.DataBind();
			}
		}

		protected void rptKPIProgressData_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			var dataItem = e.Item.DataItem as DataAccess.Models.KPI;

			if(dataItem != null)
			{
				var groupCell = e.Item.FindControl("groupCell") as System.Web.UI.HtmlControls.HtmlTableCell;

				if(e.Item.ItemIndex == 0)
				{
					var lGroup = e.Item.FindControl("lGroup") as Literal;

					lGroup.Text = TheGroup.GroupShortName;
					groupCell.RowSpan = TheGroup.ListKPIs.Count;
				}
				else
				{
					groupCell.Visible = false;
				}

				var lRowNum = e.Item.FindControl("lRowNum") as Literal;
				var lKPIContent = e.Item.FindControl("lKPIContent") as Literal;
				var lProgress = e.Item.FindControl("lProgress") as Literal;

				lRowNum.Text = (e.Item.ItemIndex + 1).ToString();
				lKPIContent.Text = dataItem.KPIContent;

				if(dataItem.TheKPIProgress != null)
				{
					if(!string.IsNullOrWhiteSpace(dataItem.TheKPIProgress.Quarter4Progress))
					{
						lProgress.Text = dataItem.TheKPIProgress.Quarter4Progress;
					}
					else if(!string.IsNullOrWhiteSpace(dataItem.TheKPIProgress.Quarter3Progress))
					{
						lProgress.Text = dataItem.TheKPIProgress.Quarter3Progress;
					}
					else if(!string.IsNullOrWhiteSpace(dataItem.TheKPIProgress.Quarter2Progress))
					{
						lProgress.Text = dataItem.TheKPIProgress.Quarter2Progress;
					}
					else if(!string.IsNullOrWhiteSpace(dataItem.TheKPIProgress.Quarter1Progress))
					{
						lProgress.Text = dataItem.TheKPIProgress.Quarter1Progress;
					}
				}
			}
		}
	}
}